package com.zyt.ssss.controller;

import com.zyt.ssss.pojo.Resume;
import com.zyt.ssss.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author zyt
 * @createDate 2020/05/14 9:20 PM
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {
    @Autowired
    private IResumeService resumeService;


    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    @ResponseBody
    public String test2() {
        return "asdfasdfasdf";
    }
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ModelAndView test() {
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public ModelAndView selectAll(Long id) {
        ModelAndView modelAndView = new ModelAndView();
        if (null == id) {
            List<Resume> resumes = resumeService.findAll();

            modelAndView.addObject("resumes", resumes);
            modelAndView.setViewName("list");
        } else {
            Resume resume = resumeService.findById(id);
            modelAndView.addObject("resume", resume);
            modelAndView.setViewName("add");
        }

        return modelAndView;
    }


    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ModelAndView insert(Resume resume) {
        Resume save = resumeService.save(resume);
        return selectAll(null);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable Long id) {
        resumeService.delete(id);
        return selectAll(null);
    }

    @RequestMapping(value = "/addPage", method = RequestMethod.GET)
    public ModelAndView addPage() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("add");
        return modelAndView;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String name, String password, HttpServletResponse response,HttpSession session) throws IOException {

        if ("admin".equals(name) && "admin".equals(password)) {
            System.out.println("合法用户");
            session.setAttribute("username",name + System.currentTimeMillis());
            return "redirect:/resume/select";
        } else {
            // 非法用户返回登录页面
            System.out.println("非法，跳转");
            return "redirect:/login.jsp";
        }

    }
}
